﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.Objects.SqlClient;
using WiseLink.RDS.Resource;

namespace CCF20Model
{
    public class DeclineStats
    {
        public string VehicleModel { get; set; }
        public int NumOfDeclineFault { get; set; }
        public int NumOfDeclineMaintenance { get; set; }
        public int NumOfDeclineCrash { get; set; }
    }
    public class DailyStats
    {
        
        public string Name1 { get; set; }
        public int Value1 { get; set; }
        public string Name2 { get; set; }
        public int Value2 { get; set; }
        
    }
    public class CareSumStats
    {
        public string MyMonth { get; set; }
        public decimal MyScore { get; set; }
    }
    public class FaultStatsByVehicleType
    {
        public int MyFaultType { get; set; }
        public string Model { get; set; }
        public int IDCCount { get; set; }
    }
    public class DeclineStatsByVehicleType
    {
        public DateTime? Buytime { get; set; }//车购买时间
        public DateTime? Createtime { get; set; }//统计时间       
        public string Jjcontent { get; set; }//拒绝原因
        public int Jjtype { get; set; }//拒绝类型
        public int? TyID { get; set; }//车型ID
    }
    public class JavaDeclineStatsByVehicleType
    {
        public string Buytime { get; set; }//车购买时间
        public string Createtime { get; set; }//统计时间       
        public string Jjcontent { get; set; }//拒绝原因
        public int Jjtype { get; set; }//拒绝类型
        public int? TyID { get; set; }//车型ID
    }
    //public class CareStats
    //{

    //    public string FaultType { get; set; }
    //    public string IDCSimNumber { get; set; }
    //    public string CodeDescription { get; set; }
    //    public string MyMonth { get; set; }
    //    public DateTime? ReceiveDate { get; set; }
    //    public DateTime? EffectiveReceiveDate { get; set; }
    //    public DateTime? FirstContactDate { get; set; }
    //    public decimal MyScore  { get; set; }

    //}
    
    
    public class StatsModel
    {
        private CCF20Entities ccf20Ent;
        
        public StatsModel()
        {
            ccf20Ent = new CCF20Entities();
        }
        public IList<V_DeviceRegistration> GetRegistrationStats()
        {
            return ccf20Ent.V_DeviceRegistration.ToList();
        }
        public IList<DeclineStats> GetDeclineStats()
        {
            return ccf20Ent.ExecuteStoreQuery<DeclineStats>("exec getdeclinestats", null).ToList();
        }
        public IList<DailyStats> GetDailyStats()
        {
            return ccf20Ent.ExecuteStoreQuery<DailyStats>("exec getdaystatistic", null).ToList();
        }
        public IList<CareSumStats> GetCareSumStats()
        {
            return ccf20Ent.ExecuteStoreQuery<CareSumStats>("exec getcarestats", null).ToList();
        }
        public IEnumerable<CareStat> GetCareStats(string month)
        {
            return ccf20Ent.CareStats.Where(c => c.MyMonth == month).OrderByDescending(c=>c.ReceiveDate);
        }
        public IList<FaultStatsByVehicleType> GetDailyFaultStatsByVehicleType()
        {

            return ccf20Ent.ExecuteStoreQuery<FaultStatsByVehicleType>("exec getfaultstatsbyvehicletype", null).ToList();
            
        }
        public IEnumerable<CareStat> GetDailyCareStats()
        {
            var query = from c in ccf20Ent.CareStats
                        where SqlFunctions.DateDiff("day", c.ReceiveDate, SqlFunctions.GetDate()) == 0
                        select c;
            return query;

        }
        public IEnumerable<DeclineStatsByVehicleType> GetDailyDelicneStatsByVehicleType()
        {
            var query = from f in ccf20Ent.Faults
                        join v in ccf20Ent.Vehicles on f.VehicleID equals v.VehicleID
                        where SqlFunctions.DateDiff("day", f.ReceiveDate, SqlFunctions.GetDate()) == 0
                              && f.CloseReason.Contains("拒绝")
                              && (f.FaultType == WiseLinkRDSStatus.Fault_Type_Fault || f.FaultType == WiseLinkRDSStatus.Fault_Type_Urgent)
                        select new DeclineStatsByVehicleType
                            {
                                Buytime = v.SaleDate,
                                Createtime = SqlFunctions.GetDate(),
                                Jjcontent = f.CloseReason,
                                TyID = v.VehicleTypeID,
                                Jjtype = f.FaultType == WiseLinkRDSStatus.Fault_Type_Fault ? 1 : 0
                            };


            return query;

        }
    }
}
